# Replication code for "Authoritarian Nostalgia, 
# Group Sentiment, and Voter Behavior: Evidence from East Asia
# Sanghoon Kim-Leffingwell

# Loading data and packages

library(grid)
library(plyr)
library(ggplot2)
library(sjPlot)

korsurvey21 <- readRDS("korsurvey_rep.rds")
twnsurvey21 <- readRDS("twnsurvey_rep.rds")

# Figures

## Figure 1
data_summary <- function(data, varname, groupnames){
  require(plyr)
  summary_func <- function(x, col){
    c(mean = mean(x[[col]], na.rm=TRUE),
      sd = sd(x[[col]], na.rm=TRUE))
  }
  data_sum<-ddply(data, groupnames, .fun=summary_func,
                  varname)
  data_sum <- rename(data_sum, c("mean" = varname))
  return(data_sum)
}

bardat_kor <- data_summary(korsurvey21, varname = "authoritarian_nostalgia",
                           groupnames = "partisanship")
bardat_kor$partisanship <- as.factor(bardat_kor$partisanship)

figure1a <- ggplot(bardat_kor, aes(x=partisanship, y=authoritarian_nostalgia, fill=partisanship)) +
  geom_bar(stat="identity",width=0.8) +
  geom_errorbar(aes(ymin=authoritarian_nostalgia-sd, ymax=authoritarian_nostalgia+sd), width=.2,
                position=position_dodge(.9)) + 
  scale_fill_manual(values=c("darkblue","red","gray80","gray80","gray80","gray80"))  +
  scale_x_discrete(labels=c("DemParty","PPP","PP","JP","Other","Ind."), name="Partisanship, Korea") + 
  labs(y="Authoritarian nostalgia scale") + theme_bw() + theme(legend.position = "none") + ylim(0,30)


bardat_twn <- data_summary(twnsurvey21, varname = "authoritarian_nostalgia",
                           groupnames = "partisanship_short")
bardat_twn$partisanship_short <- as.factor(bardat_twn$partisanship_short)

figure1b <- ggplot(subset(bardat_twn, !is.na(partisanship_short)),
       aes(x=(partisanship_short), y=authoritarian_nostalgia,fill=factor(partisanship_short))) + 
  geom_bar(stat="identity",width=0.8) +
  geom_errorbar(aes(ymin=authoritarian_nostalgia-sd, ymax=authoritarian_nostalgia+sd), width=.2,
                position=position_dodge(.9)) +
  scale_fill_manual(values=c("darkgreen","red","gray80","gray80","gray80","gray80"))  +
  scale_x_discrete(labels=c("DPP","KMT","NPP","PFP","TPP","Ind."), name="Partisanship, Taiwan") + 
  labs(y="Authoritarian nostalgia scale") + theme_bw() + theme(legend.position = "none") +
  ylim(0,30)

gridExtra::grid.arrange(figure1a, figure1b, ncol=2)

## Figure 2
### prediction plot for voting for ASP in South Korea
h5fit4 <- glm(ALPpartisan~AN01+age+female+education+ideology+
                social, data=korsurvey21)
newdata <- with(korsurvey21, data.frame(AN01 = AN01,
                                        female=mean(female,na.rm=T), 
                                        age=mean(age,na.rm=T),education=mean(education,na.rm=T),
                                        ideology=mean(ideology,na.rm=T),
                                        social=mean(social,na.rm=T)))

preds_parti <- predict(h5fit4, newdata, type="response",se.fit = T)
predf_id <- preds_parti$fit
lower_id <- preds_parti$fit - (1.96*preds_parti$se.fit) # lower bounds
upper_id <- preds_parti$fit + (1.96*preds_parti$se.fit) # upper bounds


ALP_ID_KR <- ggplot(newdata,aes(x=AN01, y= predf_id)) + geom_line() + 
  geom_ribbon(aes(ymin=lower_id, ymax=upper_id), alpha=0.2) + theme_bw() +
  labs(x = "Authoritarian nostalgia", y = " ",
       title="Prediction for ASP identification")

### prediction plot for voting for ASP in Taiwan
h5tw4 <- glm(ALPpartisan~AN01+age+female+education+ideology+social, data=twnsurvey21)
newdata_tw <- with(twnsurvey21, data.frame(AN01 = AN01,
                                           female=mean(female,na.rm=T), 
                                           age=mean(age,na.rm=T),education=mean(education,na.rm=T),
                                           ideology=mean(ideology,na.rm=T),
                                           social=mean(social,na.rm=T)))

preds_parti_tw <- predict(h5tw4, newdata_tw, type="response",se.fit = T)
predf_tw_id <- preds_parti_tw$fit
lower_tw_id <- preds_parti_tw$fit - (1.96*preds_parti_tw$se.fit) # lower bounds
upper_tw_id <- preds_parti_tw$fit + (1.96*preds_parti_tw$se.fit) # upper bounds

ALP_ID_TW <- 
  ggplot(newdata_tw,aes(x=AN01, y= predf_tw_id)) + geom_line() + 
  geom_ribbon(aes(ymin=lower_tw_id, ymax=upper_tw_id), alpha=0.2) + theme_bw() +
  labs(x = "Authoritarian nostalgia", y = " ",
       title="Prediction for ASP identification")

### prediction plot for ASP party attachment in South Korea
h5afit2<-lm(partyattach~AN01+age+female+education+ideology, data=korsurvey21)
preds_affect <- predict(h5afit2, newdata, type="response",se.fit = T)
predf <- preds_affect$fit
lower <- preds_affect$fit - (1.96*preds_affect$se.fit) # lower bounds
upper <- preds_affect$fit + (1.96*preds_affect$se.fit) # upper bounds

ALP_AF_KR <- ggplot(newdata,aes(x=AN01, y= predf)) + geom_line() + 
  geom_ribbon(aes(ymin=lower, ymax=upper), alpha=0.2) + theme_bw() +
  labs(x = "Authoritarian nostalgia", y = "",
       title="Prediction for ASP partisan attachment")

### prediction plot for ASP party attachment in Taiwan

h5atw2 <- lm(partyattach~AN01+age+female+education+ideology, data=twnsurvey21)

preds_affect_tw <- predict(h5atw2, newdata_tw, type="response",se.fit = T)
predf_tw <- preds_affect_tw$fit
lower_tw <- preds_affect_tw$fit - (1.96*preds_affect_tw$se.fit) # lower bounds
upper_tw <- preds_affect_tw$fit + (1.96*preds_affect_tw$se.fit) # upper bounds

ALP_AF_TW <- ggplot(newdata_tw,aes(x=AN01, y= predf_tw)) + geom_line() + 
  geom_ribbon(aes(ymin=lower_tw, ymax=upper_tw), alpha=0.2) + theme_bw() +
  labs(x = "Authoritarian nostalgia", y = "",
       title="Prediction for ASP partisan attachment")

### Combine together 
top1 <- textGrob(" ")
top_kr <- textGrob("Korea study",gp=gpar(fontsize=16))
top_tw <- textGrob("Taiwan study",gp=gpar(fontsize=16))
left_ID <- textGrob("Partisan \n identification")
left_AF <- textGrob("Partisan \n attachment")

figure2 <- gridExtra::grid.arrange(top1,top_kr, top_tw, 
                                         left_ID,ALP_ID_KR, ALP_ID_TW, 
                                         left_AF,ALP_AF_KR, ALP_AF_TW,
                                         ncol=3, heights=c(0.5,3,3), widths=c(0.8,3,3))

## Figure 3

cohortgraph_kr<- aggregate(AN01~cohort+partygroup, mean, data=korsurvey21)
cohortgr_kr <- ggplot(cohortgraph_kr, aes(y=AN01, x=cohort, group=factor(partygroup) )) +
  geom_line(aes(color=factor(partygroup),linetype=factor(partygroup)),linewidth=1) +
  geom_point(aes(color=factor(partygroup),shape=factor(partygroup)),size=2.5) +
  scale_shape_manual(values=c(16, 1, 2, 0, 15),
                     labels=c("Strong ASP", "Weak ASP", "Independents","Weak Dem","Strong Dem")) +
  scale_linetype_discrete(labels=c("Strong ASP", "Weak ASP", "Independents","Weak Dem","Strong Dem"))+
  ylim(0,1) + theme_bw() + 
  scale_color_manual(values=c("#8B0000","#FF6961","gray50","#779ECB", "#00008B" ),
                     labels=c("Strong ASP", "Weak ASP", "Independents","Weak Dem","Strong Dem")) +
  scale_x_continuous(breaks=seq(2,6,1),
                     labels=c("20s", "30s", "40s","50s","60s and \n older")) +
  labs(x="Cohort (age groups)", 
       y="Authoritarian nostalgia across \n partisanship and age cohorts (South Korea)")+
  theme(text=element_text(size=13),legend.position = c(0.15, 0.8),
        legend.title = element_blank(),
        legend.background = element_rect(linewidth=0.1, linetype="solid", 
                                         colour ="black"))

cohortgraph_tw<- aggregate(AN01~cohort+partygroup, mean, data=twnsurvey21)

cohortgr_tw <- ggplot(cohortgraph_tw, aes(y=AN01, x=cohort, group=factor(partygroup) )) +
  geom_line(aes(color=factor(partygroup),linetype=factor(partygroup)),linewidth=1) +
  geom_point(aes(color=factor(partygroup),shape=factor(partygroup)),size=2.5) +
  scale_shape_manual(values=c(16, 1, 2, 0, 15), 
                     labels=c("Strong ASP", "Weak ASP", "Independents","Weak Dem","Strong Dem")) +
  scale_linetype_discrete(labels=c("Strong ASP", "Weak ASP", "Independents","Weak Dem","Strong Dem"))+
  ylim(0,1) + theme_bw() +
  scale_color_manual(values=c("#00008B","#779ECB","gray50","#56ae57", "#013220" ),
                     labels=c("Strong ASP", "Weak ASP", "Independents","Weak Dem","Strong Dem")) + 
  scale_x_continuous(breaks=seq(2,6,1),
                     labels=c("20s", "30s", "40s","50s","60s and \n older")) +
  labs(x="Cohort (age groups)", 
       y="Authoritarian nostalgia across \n partisanship and age cohorts (Taiwan)") +
  theme(text=element_text(size=13),legend.position = c(0.85, 0.2),
        legend.title = element_blank(),
        legend.background = element_rect(linewidth=0.1, linetype="solid", 
                                         colour ="black"))
figure3 <- gridExtra::grid.arrange(cohortgr_kr, cohortgr_tw, ncol=2)

### Figure 4
#### Regression models
h7_1fit2 <- lm(ANdiffAuth~AN+age+female+education+ideology+social,data=korsurvey21)
h7_2fit2 <- lm(ANdiffDem~AN+age+female+education+ideology+social,data=korsurvey21)
h7_3fit2 <- lm(AuthdiffDem~AN+age+female+education+ideology+social,data=korsurvey21)

h7_1tw2 <- lm(ANdiffAuth~AN+age+female+education+ideology+social,data=twnsurvey21)
h7_2tw2 <- lm(ANdiffDem~AN+age+female+education+ideology+social,data=twnsurvey21)
h7_3tw2 <- lm(AuthdiffDem~AN+age+female+education+ideology+social,data=twnsurvey21)

model1Frame <- data.frame(Variable = "Authoritarian Nostalgia",
                          Coefficient = summary(h7_1fit2)$coef[2,1],
                          SE = summary(h7_1fit2)$coef[2,2], 
                          modelName = "AN-Auth FT difference")
model2Frame <- data.frame(Variable = rownames(summary(h7_2fit2)$coef)[2],
                          Coefficient = summary(h7_2fit2)$coef[2,1],
                          SE = summary(h7_2fit2)$coef[2,2], 
                          modelName = "AN-Dem difference")
model3Frame <- data.frame(Variable = "Authoritarian Nostalgia",
                          Coefficient = summary(h7_3fit2)$coef[2,1],
                          SE = summary(h7_3fit2)$coef[2,2], 
                          modelName = "Auth-Dem FT difference")
modeltwFrame1a <- data.frame(Variable = "Authoritarian Nostalgia",
                             Coefficient = summary(h7_2tw2)$coef[2,1],
                             SE = summary(h7_2tw2)$coef[2,2], 
                             modelName = "AN-Dem FT difference")
modeltwFrame2a <- data.frame(Variable = "Authoritarian Nostalgia",
                             Coefficient = summary(h7_1tw2)$coef[2,1],
                             SE = summary(h7_1tw2)$coef[2,2], 
                             modelName = "AN-Auth FT difference")
modeltwFrame3a <- data.frame(Variable = "Authoritarian Nostalgia",
                             Coefficient = summary(h7_3tw2)$coef[2,1],
                             SE = summary(h7_3tw2)$coef[2,2], 
                             modelName = "Auth-Dem FT difference")
lmModelFrame1 <- data.frame(rbind(model2Frame, modeltwFrame1a, model1Frame, modeltwFrame2a, 
                                  model3Frame,modeltwFrame3a))  # etc.

interval1 <- -qnorm((1-0.9)/2)
interval2 <- -qnorm((1-0.95)/2)
lmModelFrame1$factor <- c("a","b","c","d","e","f")
lmModelFrame1$country <- c("Korea", "Taiwan","Korea", "Taiwan","Korea", "Taiwan")

figure4 <- ggplot(lmModelFrame1)+ 
  geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) + 
  geom_linerange(aes(x = factor, ymin = Coefficient - SE*interval1,
                                        ymax = Coefficient + SE*interval1,color=country),
                                    lwd = 2, position = position_dodge(width = 1/2))+ 
  geom_pointrange(aes(x = factor, y = Coefficient, 
                                         ymin = Coefficient - SE*interval2,
                                         ymax = Coefficient + SE*interval2, shape=country,color=country),
                                     lwd = 1, position = position_dodge(width = 1/2),
                                     fill = "WHITE") +
  theme_classic() + scale_linetype(guide="none") + scale_shape(guide="none")+
  scale_color_manual(values=c("black","gray60"),  guide="none") + 
  theme(legend.position = "bottom",  axis.text.x=element_blank(),
        axis.ticks.x=element_blank(), text=element_text(size=12),
        panel.border = element_rect(colour = "black", fill=NA)) +
  labs(x="IV=Authoritarian nostalgia",size=12, y= "DV=Difference in feelings") + 
  ylim(-1,12.5) + geom_vline(xintercept = 2.5, color=gray(1/2)) +
  geom_vline(xintercept = 4.5, color=gray(1/2)) + 
  annotate(geom="text", x=1.5, y=11.5, label="AN-Dem pair",size=5) +
  annotate(geom="text", x=3.5, y=11.5, label="AN-Auth pair",size=5) +
  annotate(geom="text", x=5.5, y=11.5, label="Auth-Dem pair",size=5)+ 
  annotate(geom="text", x=.75, y=7, label="Korea \n study",size=4, color="gray20") +
  annotate(geom="text", x=1.75, y=7.5, label="Taiwan \n study",size=4, color="gray20") +
  labs(title="Authoritarian nostalgia and difference in candidate feelings")
figure4

### Figure 5
h7_1fit4 <- glm(ANoverAuth~AN+age+female+education+ideology+social,data=korsurvey21,
                family="binomial")
h7_2fit4 <- glm(ANoverDem~AN+age+female+education+ideology+social,data=korsurvey21,
                family="binomial")
h7_3fit4 <- glm(AuthoverDem~AN+age+female+education+ideology+social,data=korsurvey21,
                family="binomial")

h7_1tw4 <- glm(ANoverAuth~AN+age+female+education+ideology+social,data=twnsurvey21,
               family="binomial")
h7_2tw4 <- glm(ANoverDem~AN+age+female+education+ideology+social,data=twnsurvey21,
               family="binomial")
h7_3tw4 <- glm(AuthoverDem~AN+age+female+education+ideology+social,data=twnsurvey21,
               family="binomial")

model1Frame1 <- data.frame(Variable = "Authoritarian Nostalgia",
                           Coefficient = summary(h7_1fit4)$coef[2,1],
                           SE = summary(h7_1fit4)$coef[2,2], 
                           modelName = "AN-Auth selection")
model2Frame1 <- data.frame(Variable = "Authoritarian Nostalgia",
                           Coefficient = summary(h7_2fit4)$coef[2,1],
                           SE = summary(h7_2fit4)$coef[2,2], 
                           modelName = "AN-Dem selection")
model3Frame1 <- data.frame(Variable = "Authoritarian Nostalgia",
                           Coefficient = summary(h7_3fit4)$coef[2,1],
                           SE = summary(h7_3fit4)$coef[2,2], 
                           modelName = "Auth-Dem selection")

modeltwFrame1b <- data.frame(Variable = "Authoritarian Nostalgia",
                             Coefficient = summary(h7_2tw4)$coef[2,1],
                             SE = summary(h7_2tw4)$coef[2,2], 
                             modelName = "AN-Dem selection")
modeltwFrame2b <- data.frame(Variable = "Authoritarian Nostalgia",
                             Coefficient = summary(h7_1tw4)$coef[2,1],
                             SE = summary(h7_1tw4)$coef[2,2], 
                             modelName = "AN-Auth selection")
modeltwFrame3b <- data.frame(Variable = "Authoritarian Nostalgia",
                             Coefficient = summary(h7_3tw4)$coef[2,1],
                             SE = summary(h7_3tw4)$coef[2,2], 
                             modelName = "Auth-Dem selection")

lmModelFrame2 <- data.frame(rbind(model2Frame1, modeltwFrame1b, model1Frame1, modeltwFrame2b, 
                                  model3Frame1,modeltwFrame3b))  # etc.
interval1 <- -qnorm((1-0.9)/2)
interval2 <- -qnorm((1-0.95)/2)
lmModelFrame2$factor <- c("a","b","c","d","e","f")
lmModelFrame2$country <- c("Korea", "Taiwan","Korea", "Taiwan","Korea", "Taiwan")

figure5 <- ggplot(lmModelFrame2) + 
  geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) + 
  geom_linerange(aes(x = factor, ymin = Coefficient - SE*interval1,
                                        ymax = Coefficient + SE*interval1,color=country),
                                    lwd = 2, position = position_dodge(width = 1/2)) + 
  geom_pointrange(aes(x = factor, y = Coefficient, 
                                         ymin = Coefficient - SE*interval2,
                                         ymax = Coefficient + SE*interval2, shape=country,color=country),
                                     lwd = 1, position = position_dodge(width = 1/2),
                                     fill = "WHITE")+
  theme_classic() + scale_shape(guide="none")+
  scale_color_manual(values=c("black","gray60"),
                     guide="none") + theme(legend.position = "bottom", 
                                      axis.text.x=element_blank(),
                                      axis.ticks.x=element_blank(),
                                      text=element_text(size=12),
                                      panel.border = element_rect(colour = "black", fill=NA)) +
  labs(x="IV=Authoritarian nostalgia",size=12, y= "DV=Candidate selection") + 
  geom_vline(xintercept = 2.5, color=gray(1/2)) +
  geom_vline(xintercept = 4.5, color=gray(1/2)) + 
  annotate(geom="text", x=1.5, y=1.3, label="AN-Dem pair",size=5) +
  annotate(geom="text", x=3.5, y=1.3, label="AN-Auth pair",size=5) +
  annotate(geom="text", x=5.5, y=1.3, label="Auth-Dem pair",size=5)+ 
  annotate(geom="text", x=.75, y=.75, label="Korea \n study",size=4, color="gray20") +
  annotate(geom="text", x=1.75, y=.5, label="Taiwan \n study",size=4, color="gray20") +
  labs(title="Authoritarian nostalgia and candidate selection")
figure5 
